package com.ts.animal.service.impl;

import com.ts.animal.dao.InputsDao;
import com.ts.animal.dao.OestrusRecordDao;
import com.ts.animal.domain.InputsDO;
import com.ts.animal.domain.OestrusRecordDO;
import com.ts.animal.service.OestrusRecordService;
import com.ts.common.utils.DataPerUitl;
import com.ts.common.utils.Result;
import com.ts.common.utils.ShiroUtils;
import com.ts.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class OestrusRecordServiceImpl implements OestrusRecordService {
    @Autowired
    private OestrusRecordDao oestrusRecordDao;
    @Autowired
    private InputsDao inputsDao;

    @Override
    public OestrusRecordDO get(Integer id) {
        return oestrusRecordDao.get(id);
    }

    @Override
    public List<OestrusRecordDO> list(Map<String, Object> map) {
        map.putAll(DataPerUitl.deptPer());
        return oestrusRecordDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        map.putAll(DataPerUitl.deptPer());
        return oestrusRecordDao.count(map);
    }

    @Override
    public int save(Result result, OestrusRecordDO oestrusRecord) {
        int ret = 0;
        String idListStr = oestrusRecord.getRemark1();
        oestrusRecord.setRemark1(null);
        if (StringUtils.isNotEmpty(idListStr)) {
            String[] idList = idListStr.split(",");
            for (String inputId : idList) {
                InputsDO inputsDO = inputsDao.get(Integer.valueOf(inputId));
                if (inputsDO != null) {
                    oestrusRecord.setBatchNum(inputsDO.getBatchNum());
                    oestrusRecord.setDeptId(inputsDO.getDeptId());
                    oestrusRecord.setNum(inputsDO.getNum());
                    oestrusRecord.setTypeName(inputsDO.getTypeName());
                    oestrusRecord.setVarietyName(inputsDO.getVarietyName());
                    oestrusRecord.setStatus(inputsDO.getStatus());
                    oestrusRecord.setInputsId(inputsDO.getId());
                    oestrusRecord.setCreateTime(new Date());
                    oestrusRecord.setCreateUser((int) ShiroUtils.getUserId().longValue());
                    ret = +oestrusRecordDao.save(oestrusRecord);
                }
            }
        }

        if (ret == 0) {
            result.set(Result.FAIL, "保存失败", null);
        }
        return ret;


    }

    @Override
    public int update(Result result, OestrusRecordDO oestrusRecord) {
        int ret = oestrusRecordDao.update(oestrusRecord);
        if (ret == 0) {
            result.set(Result.FAIL, "更新失败", null);
        }
        return ret;
    }

    @Override
    public int remove(Result result, Integer id) {
        int ret = oestrusRecordDao.remove(id);
        if (ret == 0) {
            result.set(Result.FAIL, "删除失败", null);
        }
        return ret;
    }

    @Override
    public int batchRemove(Result result, Integer[] ids) {
        int ret = oestrusRecordDao.batchRemove(ids);
        if (ret != ids.length) {
            result.set(Result.FAIL, "部分删除失败", null);
        }
        return ret;
    }

}
